/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.swirrel.installer;

import java.awt.Component;
import java.awt.Window;
import java.awt.event.WindowListener;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import org.swirrel.annotation.WindowActivated;
import org.swirrel.annotation.WindowClosed;
import org.swirrel.annotation.WindowClosing;
import org.swirrel.annotation.WindowDeactivated;
import org.swirrel.annotation.WindowDeiconified;
import org.swirrel.annotation.WindowIconified;
import org.swirrel.annotation.WindowOpened;
import org.swirrel.listener.SwirrelWindowListener;
import org.swirrel.util.MethodCall;

/**
 *
 * @author Gronau
 */
public class WindowListenerInstaller extends AbstractListenerInstaller implements SwirrelListenerInstaller {

    public WindowListenerInstaller() {
        super(Arrays.<Class<? extends Annotation>>asList(WindowActivated.class,
                WindowClosed.class, WindowClosing.class,
                WindowDeactivated.class, WindowDeiconified.class,
                WindowIconified.class, WindowOpened.class),
                Arrays.<Class<?>>asList(Window.class));
    }

    public boolean isListenerAlreadySet(Object obj) {
        for (WindowListener ml : ((Window) obj).getWindowListeners()) {
            if (ml instanceof SwirrelWindowListener) {
                return true;
            }
        }
        return false;
    }

    public void registerListener(Field field, Component comp, List<MethodCall> methodCalls) throws IllegalAccessException {
        ((Window) field.get(comp)).addWindowListener(
                new SwirrelWindowListener(methodCalls.get(0), methodCalls.get(1),
                methodCalls.get(2), methodCalls.get(3), methodCalls.get(4),
                methodCalls.get(5), methodCalls.get(6)));
    }
}
